#!/usr/bin/env python3
# There are a variety of RFID readers out there, USB and non-USB variants.
# This might create problems in recognizing the reader you are using.
# We haven't found the silver bullet yet. If you can contribute to this
# quest, please comment in the issue thread or create pull requests.
# ALTERNATIVE SCRIPTS:
# If you encounter problems with this script Reader.py
# consider and test one of the alternatives in the same scripts folder.
# Replace the Reader.py file with one of the following files:
# * Reader.py.experimental
#     This alternative Reader.py script was meant to cover not only USB readers but more.
#     It can be used to replace Reader.py if you have readers such as
#     MFRC522, RDM6300 or PN532
# * Reader.py.kkmoonRFIDreader
#     KKMOON RFID Reader which appears twice in the devices list as HID 413d:2107
#     and this required to check "if" the device is a keyboard.


import os.path
import sys

from evdev import InputDevice, ecodes, list_devices
from select import select
import logging
logger = logging.getLogger(__name__)


def get_devices():
    return [InputDevice(fn) for fn in list_devices()]


class Reader:

    def __init__(self):
        logger.debug('Initialize Reader')
        path = os.path.dirname(os.path.realpath(__file__))
        self.keys = "X^1234567890XXXXqwertzuiopXXXXasdfghjklXXXXXyxcvbnmXXXXXXXXXXXXXXXXXXXXXXX"
        deviceNameFile = os.path.join(path, 'deviceName.txt')
        if not os.path.isfile(deviceNameFile):
            logger.error('deviceName not set in {deviceNameFile}'.format(deviceNameFile=deviceNameFile))
            sys.exit('Please run RegisterDevice.py first')
        else:
            with open(deviceNameFile, 'r') as f:
                deviceName = f.read()
                logging.debug('DeviceName {deviceName}'.format(deviceName=deviceName))
            devices = get_devices()
            for device in devices:
                if device.name == deviceName:
                    self.dev = device
                    logger.debug('Found device')
                    break
            try:
                self.dev
            except Exception:
                sys.exit('Could not find the device %s\n. Make sure is connected' % deviceName)

    def readCard(self):
        logger.debug('readCard')
        stri = ''
        key = ''
        while key != 'KEY_ENTER':
            r, w, x = select([self.dev], [], [])
            for event in self.dev.read():
                if event.type == 1 and event.value == 1:
                    stri += self.keys[event.code]
                    # print( keys[ event.code ] )
                    key = ecodes.KEY[event.code]
        return stri[:-1]
